package thread;

/**
 * 互斥性
 * 当使用多个synchronized锁定多个代码片段并且指定的同步监视器对象是同一个时,这些代码片段之间就是互斥的
 */
public class syncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t1 = new Thread() {
            public void run() {
                boo.methodA();
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                boo.methodB();
            }
        };

        t1.start();
        t2.start();

    }
}

class Boo {
    public synchronized void methodA() {//this
        Thread t = Thread.currentThread();
        System.out.println(t.getName() + "methodA正在执行");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName() + "methodA执行完毕！");
    }
    public void methodB() {
        synchronized (this) {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "methodB正在执行");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(t.getName() + "methodB执行完毕！");
        }
    }

}